package control.boardlist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.boardlist.BoardListService;
import model.boardlist.BoardListVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class BoardListController extends MultiActionController {
	private BoardListService boardListService;
	public void setBoardListService(BoardListService boardListService) {
		this.boardListService = boardListService;
	}

	/**
	 * 메뉴 관리를 위해 게시판 명을 모두 받아와 boardlist.jsp 에 출력하기 위한 메서드.
	 */
	public ModelAndView management(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<BoardListVO> blvo = boardListService.boardList();
		//System.out.println(blvo);
		return new ModelAndView("boardlist_view/boardlist", "list", blvo);
		// return new
		// ModelAndView("boardlist_view/boardlist","boardName","들어가라");
	}

	/**
	 * 게시판 추가
	 */
	public ModelAndView addBoard(HttpServletRequest request,
			HttpServletResponse response, BoardListVO sblvo) throws Exception {
		boardListService.addBoard(sblvo);
		String message=sblvo.getName()+"이 추가되었습니다.";
		return new ModelAndView("boardlist_view/result", "message", message);
	}

	/**
	 * 게시판 삭제
	 */
	public ModelAndView delBoard(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int bno = Integer.parseInt(request.getParameter("bno"));
		String bname=request.getParameter("name");
		boardListService.delBoard(bno);
		String message=bname+"이 삭제되었습니다.";
		//List<BoardListVO> blvo = boardListService.boardListByAllName();
		return new ModelAndView("boardlist_view/result", "message", message);
	}
	/**
	 * 게시판 명 수정
	 */
	public ModelAndView boardNameModify(HttpServletRequest request,
			HttpServletResponse response, BoardListVO sblvo) throws Exception {
		//System.out.println(sblvo);
		boardListService.boardNameModify(sblvo);
		//List<BoardListVO> blvo = boardListService.boardListByAllName();
		String message=sblvo.getName()+"으로 수정되었습니다.";
		return new ModelAndView("boardlist_view/result", "message", message);		
	}

	/**
	 * 게시판 순서를 변경하는 메서드
	 */
	public ModelAndView boardOrderModify(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// request.getPrarameterValues 를 사용하면 String [] 로 넘어온다.
		// ex) jsp -> boardlist.do?command=boardlist&cafe=dabang&cafe=dabang1
		// String[] bno=request.getParameterValues("cafe"); -> String[] 에
		// dabang,dabang2 값을 얻어온다.
		String[] bno = request.getParameterValues("board_no");
		String[] ono = request.getParameterValues("order_no");
		ArrayList list = new ArrayList();
		for (int i = 0; i < bno.length; i++) {
			HashMap map = new HashMap();
			map.put("board_no", bno[i]);
			map.put("board_order", ono[i]);
			list.add(map);
		}
		boardListService.boardOrderModify(list);
		//List<BoardListVO> blvo = boardListService.boardListByAllName();
		String message="순서가변경되었습니다.";
		return new ModelAndView("boardlist_view/result", "message", message);
	}
}
